<?php

namespace api\controllers;

use api\service\UserService;
use teach\util\helper\ResponseHelper;
use Yii;
use yii\filters\Cors;
use yii\rest\ActiveController;
use yii\web\Response;

class UserController extends ActiveController
{
    public $modelClass = '';

    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_JSON;
        $behaviors['corsFilter'] = [
            'class' => Cors::class,
            'cors' => [
                'Origin' => ['*'],
                'Access-Control-Request-Method' => ['POST', 'HEAD'],
                'Access-Control-Request-Headers' => ['*'],
                'Access-Control-Allow-Credentials' => true,
                'Access-Control-Max-Age' => 86400,
            ],
        ];
        return $behaviors;
    }

    /**
     * 用户信息
     */
    public function actionInfo()
    {
        $request = Yii::$app->request;
        $uid = $request->post('uid');

        $msg = '';
        $flag = false;
        if (!$uid) {
            $msg = 'uid 不能为空';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(400, $msg);
        } else {
            //通过uid获取公众号用户表跟小程序连表，如果查询无此人返回exception
            /**
            $userService = new UserService();
            $user = $userService->userPubMin($uid);
            if ($user) {
                $todayProfit = $userService->userTodayProfit($user['mini_id']);
                $user['today_profit'] = $todayProfit;
                $waitProfit = $userService->userWaitProfit($user['mini_id']);
                $user['wait_profit'] = $waitProfit;
                $appear = $userService->userAppear($user['mini_id']);
                $user['appear'] = $appear;
                $jsonData = ResponseHelper::buildJsonData(400, '成功', $user);
            } else {
                $qqURL = "https://mp.weixin.qq.com";
                header("Location:{$qqURL}");//TODO 返回到一个错误页面, 让用户扫码或者其他方式跳转到小程序再进来
                $jsonData = ResponseHelper::buildJsonData(302, '跳转');
            }
             * */

            $userService = new UserService();
            $user = $userService->userMin($uid);
            if ($user) {
                $todayProfit = $userService->userTodayProfit($user['mini_id']);
                $user['today_profit'] = $todayProfit;
                $waitProfit = $userService->userWaitProfit($user['mini_id']);
                $user['wait_profit'] = $waitProfit;
                $appear = $userService->userAppear($user['mini_id']);
                $user['appear'] = $appear;
                $jsonData = ResponseHelper::buildJsonData(200, '成功', $user);
            } else {
                $jsonData= ResponseHelper::buildJsonData(400, '失败');
            }

        }
        return $this->asJson($jsonData);
    }

    /**
     * 收益详细
     */
    public function actionProfitDetail()
    {
        $request = Yii::$app->request;
        $uid = $request->post('uid');
        $where = $request->post('where');//1.全部 2.今日收益 3.已到账收益 4.待到账收益
        $out_trade_no = $request->post('out_trade_no');//订单号
        $timeStart = $request->post('time_start'); //开始时间
        $timeEnd = $request->post('time_end'); //结束时间

        $msg = '';
        $flag = false;
        if (!$uid) {
            $msg = 'uid不能为空';
        } elseif (!$where) {
            $msg = '条件不能为空';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(400, $msg);
        } else {
            $page = $request->post('page');
            $page = $page ? $page : 1;
            $andWhere = " uid = {$uid} ";
            if ($timeStart) {
                $andWhere .= " AND create_time > {$timeStart} ";
            }
            if ($timeEnd) {
                $timeEnd = 24 * 60 * 60 + $timeEnd;
                $andWhere .= " AND create_time < {$timeEnd} ";
            }
            if ($out_trade_no) {
                $andWhere .= " AND out_trade_no = {$out_trade_no} ";
            }
            if ($where == 2) {
                $andWhere .= " AND (status = 1 OR status = 2)";
            }
            if ($where == 3) {
                $andWhere .= " AND status = 1 ";
            }
            if ($where == 4) {
                $andWhere .= " AND status = 2 ";
            }
            $user = new UserService();
            $profitLists = $user->userProfit($andWhere, $page, $pre_page = 10);
            $jsonData = ResponseHelper::buildJsonData(200, '成功', $profitLists);
        }
        return $this->asJson($jsonData);
    }

    /**
     * 用户发起提现
     * @return Response
     */
    public function actionCash()
    {
        $request = Yii::$app->request;
        $uid = $request->post('uid');
        $amount = $request->post('amount');

        $msg = '';
        $flag = false;
        if (!$uid) {
            $msg = 'uid 不能为空';
        } elseif (!$amount) {
            $msg = 'amount 不能为空';
        }elseif ($amount < 1) {
            $msg = '金额不能小于1,每次提取范围1~5000';
        }elseif ($amount > 5000) {
            $msg = '金额不能大于5000,每次提取范围1~5000';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(400, $msg);
        } else {
            $userService = new UserService();
            $where = " id = {$uid} ";
            $user = $userService->userOne($where);
            if ($user) {
                if ($user['mall_total_fee'] < $amount) {
                    $jsonData = ResponseHelper::buildJsonData(401, '对不去，您的金额有误');
                    return $this->asJson($jsonData);
                }

                $map = [
                    'openid' => $user['openid'],
                    'uid' => $uid,
                    'amount' => $amount,
                    'status' => 1,
                    'create_time' => time(),
                    'order_num' => "NO." . date("YmdHis", time()) . time(),
                    'mid'=> $user['mall_id'],
                ];
                $res = $userService->userCashAppear($map);

                if ($res) {
                    $jsonData = ResponseHelper::buildJsonData(200, '成功');
                } else {
                    $jsonData = ResponseHelper::buildJsonData(400, '失败,请重试或者联系商家');
                }
            } else {
                $jsonData = ResponseHelper::buildJsonData(400, '用户信息有误');
            }
        }
        return $this->asJson($jsonData);
    }

    /**
     * 用户提现记录
     * @return Response
     */
    public function actionCashDetail()
    {
        $request = Yii::$app->request;
        $uid = $request->post('uid');
        $status = $request->post('status'); //o全部1提现中2成功3失败

        $msg = '';
        $flag = false;
        if (!$uid) {
            $msg = 'uid 不能为空';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(400, $msg);
        } else {
            $page = $request->post('page');
            $page = $page ? $page : 1;
            $map = " uid = {$uid} ";
            if ($status) {
                $map .= " AND status = {$status} ";
            }
            $userService = new UserService();
            $appearList = $userService->userAppearDetail($map, $page, $pre_page = 10);
            $jsonData = ResponseHelper::buildJsonData(200, '成功', $appearList);
        }
        return $this->asJson($jsonData);
    }

}